﻿<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Uc_department_fv.ascx.vb"
    Inherits="Apps_Shares_Uc_department_fv"  %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:FormView ID="fv" runat="server" DataSourceID="fvSrc" Width="100%">
    <InsertItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center"
            width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <%--====================== BEGIN ROW HERE ======================--%>
                <tr>
                    <td align="right" class="td_box">
                        ระดับหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:DropDownList ID="ddlDepartmentLevel" runat="server" AppendDataBoundItems="true"
                            DataSourceID="ddlDepartmentLevelSrc" DataTextField="Text" DataValueField="Value"
                            AutoPostBack="false" SelectedValue='<%# Bind("master_department_code") %>'
                            onselectedindexchanged="ddlDepartmentLevel_SelectedIndexChanged">
                            <asp:ListItem Text='ระดับหน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="ddlDepartmentLevelSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                            SelectCommand="select  [Text] = master_department_name ,
		                                [Value] = master_department_code
                                from	master_department m
                                where	master_department_status = 1 and
                                        master_department_level >
                                        (select  case master_department_level when 1 then 0 else master_department_level end
                                        from	master_department m
                                        inner join stp_department d on d.master_department_code = m.master_department_code
                                        inner join staff s on s.department_code = d.department_code
                                        where s.staff_code = @staff_code)">
                                <SelectParameters>
                                    <asp:SessionParameter SessionField="staff_code" Name="staff_code" />
                                </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานผู้บังคับบัญชา :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:DropDownList ID="ddlHeadDepartment" DataSourceID="ddlHeadDepartmentSrc" runat="server" DataTextField="department_name"
                            DataValueField="department_code" SelectedValue='<%# Bind("department_parent") %>' AppendDataBoundItems="True">
                            <asp:ListItem Text='ระดับหน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:ObjectDataSource ID="ddlHeadDepartmentSrc" runat="server" SelectMethod="GetStpDepartmentByPrivilege"
                            TypeName="CodeMatrix.Business.GenericData"></asp:ObjectDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หมายเลขหน่วยงาน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:TextBox ID='txtDepartmentId' runat='server' CssClass='text_normal' Text='<%# Bind("department_number") %>'
                            MaxLength="10"></asp:TextBox>
                        (9999999999)
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:TextBox ID='txtDepartmentName' runat='server' CssClass='text_normal' Text='<%# Bind("department_name") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:RadioButtonList ID='rblStatus' runat='server' SelectedValue='<%# Bind("department_status") %>'
                            RepeatDirection="Horizontal" RepeatLayout='Flow'>
                            <asp:ListItem Text='ใช้งาน' Value='1' Selected='True'></asp:ListItem>
                            <asp:ListItem Text='ไม่ใช้งาน' Value='2'></asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <%--====================== END ROW HERE ======================--%>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnSubmit' runat='server' Text='บันทึก' ValidationGroup='g1' CommandName='Insert' />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                        <div class="hide">
                            <asp:RequiredFieldValidator ID="txtDepartmentName_RequiredFieldValidator" runat="server"
                                ControlToValidate="txtDepartmentName" ErrorMessage="กรุณากรอกชื่อหน่วยงาน" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlDepartmentLevel_RequiredFieldValidator" runat="server"
                                ControlToValidate="ddlDepartmentLevel" ErrorMessage="กรุณาเลือกระดับหน่วยงาน"
                                InitialValue="0" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="rblStatus"
                                ErrorMessage="<%$ Resources:Resource, MSG_REQ_STATUS %>" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:CustomValidator ID="txtDepartmentId_CustomValidator" runat="server" ControlToValidate="txtDepartmentId"
                                ClientValidationFunction="checkDepartmentNumber" EnableClientScript="true" ErrorMessage="หน่วยงานนี้มีอยู่ในระบบแล้ว"
                                ValidationGroup="g1"></asp:CustomValidator>
                            <asp:ValidationSummary ID="fvValidationSummary" runat="server" ShowMessageBox="True"
                                ShowSummary="True" ValidationGroup="g1" />
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </InsertItemTemplate>
    <EditItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center"
            width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <%--====================== BEGIN ROW HERE ======================--%>
                <tr>
                    <td align="right" class="td_box">
                        ระดับหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID="ddlDepartmentLevel" runat="server" AppendDataBoundItems="true"
                            DataSourceID="ddlDepartmentLevelSrc" DataTextField="Text" DataValueField="Value"
                            AutoPostBack="false" SelectedValue='<%# Bind("master_department_code") %>'
                            onselectedindexchanged="ddlDepartmentLevel_SelectedIndexChanged">
                            <asp:ListItem Text='ระดับหน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="ddlDepartmentLevelSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
                            SelectCommand="select  [Text] = master_department_name ,
		                                [Value] = master_department_code
                                from	master_department m
                                where	master_department_status = 1 and
                                        master_department_level >=
                                        (select  master_department_level
                                        from	master_department m
                                        inner join stp_department d on d.master_department_code = m.master_department_code
                                        inner join staff s on s.department_code = d.department_code
                                        where s.staff_code = @staff_code)">
                                <SelectParameters>
                                    <asp:SessionParameter SessionField="staff_code" Name="staff_code" />
                                </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานผู้บังคับบัญชา :
                    </td>
                    <td align="left" class="td_box">
                        <asp:DropDownList ID="ddlHeadDepartment" DataSourceID="ddlHeadDepartmentSrc" 
                            runat="server" DataTextField="department_name"
                            DataValueField="department_code" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("department_parent") %>'>
                            <asp:ListItem Text='ระดับหน่วยงาน' Value='0'></asp:ListItem>
                        </asp:DropDownList>
                        <asp:ObjectDataSource ID="ddlHeadDepartmentSrc" runat="server"
                                SelectMethod="GetStpDepartmentByPrivilege" 
                                TypeName="CodeMatrix.Business.GenericData">
                        </asp:ObjectDataSource>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หมายเลขหน่วยงาน :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtDepartmentId' runat='server' CssClass='text_normal' Text='<%# Bind("department_number") %>'
                            MaxLength="10"></asp:TextBox>
                        (9999999999)
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:TextBox ID='txtDepartmentName' runat='server' CssClass='text_normal' Text='<%# Bind("department_name") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        <asp:RadioButtonList ID='rblStatus' runat='server' SelectedValue='<%# Bind("department_status") %>'
                            RepeatDirection="Horizontal" RepeatLayout='Flow'>
                            <asp:ListItem Text='ใช้งาน' Value='1' Selected='True'></asp:ListItem>
                            <asp:ListItem Text='ไม่ใช้งาน' Value='2'></asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่สร้างข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrCreate' runat='server' Text='<%# Eval("Creator") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่ปรับปรุงข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrUpdate' runat='server' Text='<%# Eval("Editor") %>'></asp:Literal>
                    </td>
                </tr>
                <%--====================== END ROW HERE ======================--%>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnSubmit' runat='server' Text='บันทึก' CommandName='Update' ValidationGroup="g1" />
                        <asp:Button ID='btnCancel' runat='server' Text='ยกเลิก' PostBackUrl="~/Apps/department_list.aspx?Edit=true" />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                        <div class="hide">
                            <asp:RequiredFieldValidator ID="txtDepartmentName_RequiredFieldValidator" runat="server"
                                ControlToValidate="txtDepartmentName" ErrorMessage="กรุณากรอกชือระดับหน่วยงาน"
                                ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="ddlDepartmentLevel_RequiredFieldValidator" runat="server"
                                ControlToValidate="ddlDepartmentLevel" ErrorMessage="กรุณาเลือกระดับหน่วยงาน"
                                InitialValue="0" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="rblStatus"
                                ErrorMessage="<%$ Resources:Resource, MSG_REQ_STATUS %>" InitialValue="0" ValidationGroup="g1"></asp:RequiredFieldValidator>
                            <br />
                            <%--<asp:CustomValidator ID="txtDepartmentId_CustomValidator" runat="server" ControlToValidate="txtDepartmentId" 
                                ClientValidationFunction="checkDepartmentNumber" EnableClientScript="true"
                                ErrorMessage="หน่วยงานนี้มีอยู่ในระบบแล้ว" ValidationGroup="g1"></asp:CustomValidator>--%>
                            <asp:ValidationSummary ID="fvValidationSummary" runat="server" ShowMessageBox="True"
                                ShowSummary="True" ValidationGroup="g1" />
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </EditItemTemplate>
    <ItemTemplate>
        <table cellspacing="2" cellpadding="2" bordercolor="#cccccc" border="1" align="center"
            width="60%">
            <tbody>
                <tr>
                    <td bgcolor="#999999" align="center" class="th_data" colspan="2">
                        &nbsp;
                    </td>
                </tr>
                <%--====================== BEGIN ROW HERE ======================--%>
                <tr>
                    <td align="right" class="td_box">
                        ระดับหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID="ddlDepartmentLevel" runat="server" Text='<%# CodeMatrix.Business.GenericData.GetMasterDepartmentLabel(me,Eval("master_department_code")) %>'>
                                
                        </asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หน่วยงานผู้บังคับบัญชา :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ddlHeadDepartment' runat='server' Text='<%#CodeMatrix.Business.GenericData.GetDepartmentLabel(me,Eval("department_parent")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        หมายเลขหน่วยงาน :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='txtDepartmentId' runat='server' Text='<%# Bind("department_number") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อหน่วยงาน <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='txtDepartmentName' runat='server' Text='<%# Bind("department_name") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        สถานะ <span class="star">*</span> :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='rblStatus' runat='server' Text='<%# CodeMatrix.Business.GenericData.GetStatusLabel(Eval("department_status")) %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่สร้างข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrCreate' runat='server' Text='<%# Eval("Creator") %>'></asp:Literal>
                    </td>
                </tr>
                <tr>
                    <td align="right" class="td_box">
                        ชื่อและวันเวลาที่ปรับปรุงข้อมูล :
                    </td>
                    <td align="left" class="td_box">
                        &nbsp;
                        <asp:Literal ID='ltrUpdate' runat='server' Text='<%# Eval("Editor") %>'></asp:Literal>
                    </td>
                </tr>
                <%--====================== END ROW HERE ======================--%>
                <tr>
                    <td bgcolor="#999999" align="center" class="td_data_end" colspan="2">
                        <asp:Button ID='btnCancel' runat='server' Text='ย้อนกลับ' PostBackUrl="~/Apps/department_list.aspx" />
                        <asp:Button ID='btnMainMenu' Text='<%$ Resources:Resource,BTN_MAINMENU %>' runat='server' PostBackUrl="~/Apps/ViewSystem.aspx?Edit=2" />
                    </td>
                </tr>
            </tbody>
        </table>
    </ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="fvSrc" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    InsertCommand="INSERT INTO dbo.stp_department(department_number, department_name, master_department_code, department_parent, department_status, staff_code_create, create_datetime, staff_code_update, update_datetime) VALUES (@department_number, @department_name, @master_department_code, @department_parent, @department_status, @staff_code_create, GETDATE(), @staff_code_update, GETDATE())"
    SelectCommand="SELECT [department_code]
      ,[department_number]
      ,[department_name]
      ,[master_department_code]
      ,ISNULL([department_parent], 0) department_parent
      ,[department_status]
      ,[staff_code_create]
      ,[create_datetime]
      ,[staff_code_update]
      ,[update_datetime], Creator = dbo.UFN_GetAuditor(staff_code_create,create_datetime) , Editor = dbo.UFN_GetAuditor(staff_code_update,update_datetime)
from stp_department where department_code = @department_code" 
UpdateCommand="UPDATE dbo.stp_department SET department_number = @department_number, department_name = @department_name, master_department_code = @master_department_code, department_status = @department_status, department_parent = @department_parent, update_datetime = GETDATE(), staff_code_update = @staff_code_update WHERE (department_code = @department_code)">
    <InsertParameters>
        <asp:Parameter Name="department_number" />
        <asp:Parameter Name="department_name" />
        <asp:Parameter Name="master_department_code" />
        <asp:Parameter Name="department_parent" />
        <asp:Parameter Name="department_status" />
        <asp:SessionParameter Name="staff_code_create" SessionField="staff_code" />
        <asp:SessionParameter DefaultValue="" Name="staff_code_update" SessionField="staff_code" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="department_code" QueryStringField="Edit" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="department_number" />
        <asp:Parameter Name="department_name" />
        <asp:Parameter Name="master_department_code" />
        <asp:Parameter Name="department_status" />
        <asp:Parameter Name="department_parent" />
        <asp:QueryStringParameter Name="department_code" QueryStringField="Edit" />
        <asp:SessionParameter Name="staff_code_update" SessionField="staff_code" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="hideDepartmentCode" runat="server" />

<script src="../Scripts/json2.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {

    });

    function checkDepartmentNumber(oSrc, args) {
        var id = '#<%=DepartmentID %>';
        var jData = new Object();
        jData.department_number = $(id).val();

        $.ajax({
            type: "POST",
            cache: false,
            async: false,
            data: JSON.stringify(jData, function(key, value) { return value; }),
            url: "../WebService.asmx/checkDuplicateDepartmentNo",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Server error \r\n" + XMLHttpRequest.responseText); },
            success: function(response) {
                var result = eval('(' + response.d + ')');
                if (result.Result == 'false')
                    args.IsValid = false;
                else
                    args.IsValid = true;
            }
        });
    }
</script>

